<template>
  <div>
    <el-card shadow="never" class="card">
      <el-form ref="form" :model="page">
        <el-form-item label="商品类型搜索：">
          <el-input v-model="page.className" class="elinput1" clearable placeholder="请输入" />
          <el-button type="primary" class="inquire" icon="el-icon-search" size="small" @click="inquire">查询</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card shadow="never">
      <el-button class="btn1" size="small" icon="el-icon-circle-plus-outline" @click="addList">新建</el-button>
      <el-table
        :data="queryList"
        style="width: 100%"
      >
        <el-table-column label="序号" type="index" />
        <el-table-column
          prop="className"
          label="商品类型名称"
        />
        <el-table-column
          label="操作"
          width="150"
        >
          <template #default="{row}">
            <el-link type="primary" @click="editList(row)">修改</el-link>
            <el-link type="danger" @click="deleList(row)">删除</el-link>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog
      title="新增商品类型"
      :visible.sync="dialogVisible"
      width="35%"
      @close="addClose"
    >
      <el-form ref="ruleForm" :model="addFrom" :rules="addrules" label-width="130px" class="demo-ruleForm">
        <el-form-item label="商品类型名称：" prop="className">
          <el-input v-model="addFrom.className" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmTheAddition">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改 -->
    <el-dialog
      title="修改商品类型"
      :visible.sync="editdialog"
      width="30%"
    >
      <el-form ref="ruleForm" :model="editFrom" :rules="editrules" label-width="130px" class="demo-ruleForm">
        <el-form-item label="商品类型名称：" prop="className">
          <el-input v-model="editFrom.className" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editdialog = false">取 消</el-button>
        <el-button type="primary" @click="determineTheModifications">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { beverageMachineAPI, addListAPI, editListAPI, deleListAPI } from '@/api/sku.js'
export default {
  data() {
    return {
      page: {
        className: '',
        pageIndex: 1,
        pageSize: 10
      },
      queryList: [],
      dialogVisible: false,
      editdialog: false,
      addFrom: {
        className: ''
      },
      editFrom: {
        className: ''
      },
      addrules: {
        className: [
          { required: true, message: '请输入商品名称！', trigger: 'blur' }
        ]
      },
      editrules: {
        className: [
          { required: true, message: '请输入商品名称！', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getbeverageMachineAPI()
  },
  methods: {
    // 查询
    inquire() {
      this.getbeverageMachineAPI()
    },
    // 添加
    addList() {
      this.dialogVisible = true
    },
    async getbeverageMachineAPI() {
      const res = await beverageMachineAPI(this.page)
      this.queryList = res.currentPageRecords
      console.log(this.queryList)
    },
    async confirmTheAddition() {
      await addListAPI(this.addFrom)
      this.dialogVisible = false
      this.$message.success('添加成功～')
      this.getbeverageMachineAPI()
    },
    addClose() {
      this.$refs.ruleForm.resetFields()
    },
    editList(row) {
      this.editdialog = true
      this.rid = row.classId
      this.editFrom.className = row.className
      console.log(this.editList.className)
    },
    async  determineTheModifications() {
      await editListAPI(this.rid, this.editFrom)
      this.$message.success('修改成功～')
      this.editdialog = false
      this.getbeverageMachineAPI()
    },
    async deleList(row) {
      const prve = await this.$confirm('确认删除该商品?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (prve !== 'confirm') return this.$message.info('已取消删除')
      await deleListAPI(row.classId)
      this.$message.success('删除成功！')
      this.getbeverageMachineAPI()
    }
  }
}
</script>

<style lang="scss" scoped>

.card {
  margin-bottom: 15px;
  height: 85px;

}
.el-btn {
  margin-bottom: 15px;
  background: linear-gradient(135deg, #ff9743, #ff5e20) !important;
  color: #fff;
  height: 36px;
}
.elinput1{
  width: 200px;
   height: 30px;
   line-height: 20px;
   margin-right: 20px;
   ::v-deep .el-input__inner {
    height: 30px !important;
    line-height: 30px !important;
   }
}
.inquire{
  background-color: #5f84ff !important;
  height: 30px;
  // width: 70px;
}
.btn1{
  background: linear-gradient(135deg, #ff9743, #ff5e20) !important;
    border: 0;
    color: #fff;

}
.el-link{
  margin-right: 15px;
}
.el-select {
  width: 100%;
}
</style>
